options compress=YES;
libname manasi ''; 

* PREP OBSERVATIONS FROM 2002-2005 STACKED FILE THAT ARE CDR-ELIGIBLE;
data manasi.kids_cdryrs_mergecdr_clean;
	set manasi.kids_cdryrs_mergeall_full;
	if fy=2002 | fy=2003 | fy=2004 | fy=2005;

	* Rename variables ;
	rename 	dep_comp=cdr_hold
			cdr_dt=cdr_reldate
			stil_open=cdr_open
			dds_dt=cdr_dds_date
			init_det=cdr_dds_det
			recon_dt=cdr_recon_date
			recon_det=cdr_recon_det
			alj_dt=cdr_alj_date
			alj_det=cdr_alj_det
			ac_dt=cdr_ac_date
			ac_det=cdr_ac_det
			state_cd=cdr_oldstate
			new_state=cdr_state
			body_sys=cdr_bodysys
			adj_med=cdr_mdr
			med_re=cdr_mddate
			pri_imp_cd=cdr_imppri
			sec_imp_cd=cdr_impsec
			sex_cdr=cdr_sex
			dob_cdr=cdr_dob
			dobyy_cdr=cdr_dobyy
			from_kidnumi=kiddeath;
run;

data manasi.kids_cdryrs_mergecdr_clean;
	set manasi.kids_cdryrs_mergecdr_clean;
	* Label variables ;
	label
			cdr_hold="Deputy Commissioner Hold (Y/N)"
			cdr_reldate="Date of CDR release"
			cdr_open="CDR Still Open (Y/N)"
			cdr_dds_date="Date of Initial DDS Det-1"
			cdr_dds_det="Initial DDS Det Decision-1 (F/U)"
			cdr_recon_date="Date of Reconsideration Det-2"
			cdr_recon_det="Reconsideration Decision-2 (F/U)"
			cdr_alj_date="Date of ALJ Decision-3"
			cdr_alj_det="ALJ Decision-3 (F/U)"
			cdr_ac_date="Date of AC Decision-4"
			cdr_ac_det="AC Decision-4 (F/U)"
			cdr_state="State used by ADR"
			cdr_bodysys="Body System Code"
			cdr_mdr="MDR in CDR data"
			cdr_mddate="Medical Diary Date in CDR data"
			cdr_imppri="Primary Impairment in CDR data"
			cdr_impsec="Secondary Impairment in CDR data";
	
	drop fromcdr fromssr;

	rename cdr_state=cdr_state_str; 
	rename n_firstpay_yr=n_firstpayyr;

	* Create new diagnosis variables (since original come from SSR);
	%macro rename(stub);
		rename 	n_diag1_&stub.=n_diag1ssr_&stub.
	%mend rename;

	%rename(none);
	%rename(infec);
	%rename(neo);
	%rename(endo);
	%rename(blood);
	%rename(mental);
	%rename(nerv);
	%rename(sense);
	%rename(circ);
	%rename(resp);
	%rename(dig);
	%rename(gu);
	%rename(preg);
	%rename(skin);
	%rename(musc);
	%rename(cong);
	%rename(nat);
	%rename(ill);
	%rename(inj);

	cdr_imppri=input(cdr_imppri,5.);
	hun_num=input(hun,9.);

	* CHANGE MISSING VALUES FOR HOUSEHOLD VARIABLES (DI & SSI RECEIPT) TO ZERO:
		THESE PEOPLE WEREN'T MATCHED TO MBR AND SSR WHICH MEANS NO RECEIPT;
	%macro miss(var);
		if &var._1985=. then &var._1985=0;
		if &var._1986=. then &var._1986=0;
		if &var._1987=. then &var._1987=0;
		if &var._1988=. then &var._1988=0;
		if &var._1989=. then &var._1989=0;
		if &var._1990=. then &var._1990=0;
		if &var._1991=. then &var._1991=0;
		if &var._1992=. then &var._1992=0;
		if &var._1993=. then &var._1993=0;
		if &var._1994=. then &var._1994=0;
		if &var._1995=. then &var._1995=0;
		if &var._1996=. then &var._1996=0;
		if &var._1997=. then &var._1997=0;
		if &var._1998=. then &var._1998=0;
		if &var._1999=. then &var._1999=0;
		if &var._2000=. then &var._2000=0;
		if &var._2001=. then &var._2001=0;
		if &var._2002=. then &var._2002=0;
		if &var._2003=. then &var._2003=0;
		if &var._2004=. then &var._2004=0;
		if &var._2005=. then &var._2005=0;
		if &var._2006=. then &var._2006=0;
		if &var._2007=. then &var._2007=0;
		if &var._2008=. then &var._2008=0;
		if &var._2009=. then &var._2009=0;
		if &var._2010=. then &var._2010=0;
		if &var._2011=. then &var._2011=0;
	%mend miss;

	%miss(n_hhmbayr);
	%miss(n_hhmbayr0206);
	%miss(n_hhdiindyr);
	%miss(n_hhdiindyr0206);
	%miss(n_hhcombpay);
	%miss(n_hhcombpay0206);
	%miss(n_hhonssi);
	%miss(n_hhonssi0206);
	%miss(n_sibpayall);
	%miss(n_sibnumon);
	%miss(n_par831di);
	%miss(n_par831di0206);
	%miss(n_par831ssi);
	%miss(n_par831ssi0206);
	%miss(n_kid831_di);
	%miss(n_kid831_ssi);

	%macro miss1992(var);
		if &var._1992=. then &var._1992=0;
		if &var._1993=. then &var._1993=0;
		if &var._1994=. then &var._1994=0;
		if &var._1995=. then &var._1995=0;
		if &var._1996=. then &var._1996=0;
		if &var._1997=. then &var._1997=0;
		if &var._1998=. then &var._1998=0;
		if &var._1999=. then &var._1999=0;
		if &var._2000=. then &var._2000=0;
		if &var._2001=. then &var._2001=0;
		if &var._2002=. then &var._2002=0;
		if &var._2003=. then &var._2003=0;
		if &var._2004=. then &var._2004=0;
		if &var._2005=. then &var._2005=0;
		if &var._2006=. then &var._2006=0;
		if &var._2007=. then &var._2007=0;
		if &var._2008=. then &var._2008=0;
		if &var._2009=. then &var._2009=0;
		if &var._2010=. then &var._2010=0;
		if &var._2011=. then &var._2011=0;
	%mend miss1992;

	%miss1992(n_sibappnum);

	if n_hhcombpay_2012=. then n_hhcombpay_2012=0;
	if n_hhcombpay0206_2012=. then n_hhcombpay0206_2012=0;
	if n_hhonssi_2012=. then n_hhonssi_2012=0;
	if n_hhonssi0206_2012=. then n_hhonssi0206_2012=0;
	if n_sibpayall_2012=. then n_sibpayall_2012=0;
	if n_sibnumon_2012=. then n_sibnumon_2012=0;
	if n_sibappnum_2012=. then n_sibappnum_2012=0;

run;

data manasi.kids_cdryrs_mergecdr_clean;
	set manasi.kids_cdryrs_mergecdr_clean;

* CREATE VARIABLES ON CDR ELIGIBILITY/RELEASE/COMPLETION ;

	* CDR hold dummy;
	if cdr_hold="Y" then n_cdr_hold=1;
		else if cdr_hold="N" then n_cdr_hold=0;

	* CDR release dummy;
	if (cdr_reldate>0 & cdr_reldate^=.) then n_cdr_rel=1;
		else n_cdr_rel=0;

	* CDR completed dummy ;
	if (cdr_open="N" & n_cdr_rel=1) then n_cdr_done = 1;
		else n_cdr_done = 0;
	

* CREATE/PREP COVARIATES;

	* Male dummy;
	if cdr_sex="M" then n_male=1;
		else if cdr_sex="F" then n_male=0;

	* State variable ;
	cdr_state = input(cdr_state_str,2.0);

	* YOB and first year already exist; 

	* Age at first payment ;
	n_firstpayage=n_firstpayyr-dobyy;

	* Create MDR dummies ;
	if cdr_mdr="3" then n_dibmdrcat=3;
		else if cdr_mdr="5" then n_dibmdrcat=5;
		else if cdr_mdr="7" then n_dibmdrcat=7;
	if n_dibmdrcat=. then n_dibmdrcat=99;

	* Body system variable ;
	if cdr_bodysys="@@" then cdr_bodysys=".";
	cdr_bodysys=input(cdr_bodysys,2.0);

	* Replace SSR diagnoses with OQP diagnoses;
	%macro imp(stub,low,high);
		if (cdr_imppri>=&low. & cdr_imppri<&high.) then n_diag1_&stub.=1;
			else n_diag1_&stub.=0;
		if (cdr_impsec>=&low. & cdr_impsec<&high.) then n_diag2_&stub.=1;
			else n_diag2_&stub.=0;
	%mend imp;

	if (cdr_imppri=.) then n_diag1_none=1;
		else n_diag1_none=0;
	if (cdr_impsec=.) then n_diag2_none=1;
		else n_diag2_none=0;

	%imp(infec,0,1400);
	%imp(neo,1400,2400);
	%imp(endo,2400,2800);
	%imp(blood,2800,2900);
	%imp(mental,2900,3200);
	%imp(nerv,3200,3600);
	%imp(sense,3600,3900);
	%imp(circ,3900,4600);
	%imp(resp,4600,5200);
	%imp(dig,5200,5800);
	%imp(gu,5800,6300);
	%imp(preg,6300,6800);
	%imp(skin,6800,7100);
	%imp(musc,7100,7400);
	%imp(cong,7400,7600);
	%imp(nat,7600,7800);
	%imp(ill,7800,8000);
	%imp(inj,8000,10000);

run;


* PREP OBSERVATIONS in 2006-2007 STACKED FILE THAT ARE CDR-ELIGIBLE;
data manasi.kids_cdryrs0607_mergeall_full;
	set manasi.kids_cdr06_mergeall_full
		manasi.kids_cdr07_mergeall_full			
		;
run;

data manasi.kids_cdryrs0607_mergecdr_clean;
	set manasi.kids_cdryrs0607_mergeall_full;
	if fy=2006 | fy=2007 ;

	* Rename variables ;
	rename 	dep_comp=cdr_hold
			cdr_dt=cdr_reldate
			stil_open=cdr_open
			dds_dt=cdr_dds_date
			init_det=cdr_dds_det
			recon_dt=cdr_recon_date
			recon_det=cdr_recon_det
			alj_dt=cdr_alj_date
			alj_det=cdr_alj_det
			ac_dt=cdr_ac_date
			ac_det=cdr_ac_det
			state_cd=cdr_oldstate
			new_state=cdr_state
			body_sys=cdr_bodysys
			adj_med=cdr_mdr
			med_re=cdr_mddate
			pri_imp_cd=cdr_imppri
			sec_imp_cd=cdr_impsec
			sex_cdr=cdr_sex
			dob_cdr=cdr_dob
			dobyy_cdr=cdr_dobyy
			from_kidnumi=kiddeath;
run;

data manasi.kids_cdryrs0607_mergecdr_clean;
	set manasi.kids_cdryrs0607_mergecdr_clean;
	* Label variables ;
	label
			cdr_hold="Deputy Commissioner Hold (Y/N)"
			cdr_reldate="Date of CDR release"
			cdr_open="CDR Still Open (Y/N)"
			cdr_dds_date="Date of Initial DDS Det-1"
			cdr_dds_det="Initial DDS Det Decision-1 (F/U)"
			cdr_recon_date="Date of Reconsideration Det-2"
			cdr_recon_det="Reconsideration Decision-2 (F/U)"
			cdr_alj_date="Date of ALJ Decision-3"
			cdr_alj_det="ALJ Decision-3 (F/U)"
			cdr_ac_date="Date of AC Decision-4"
			cdr_ac_det="AC Decision-4 (F/U)"
			cdr_state="State used by ADR"
			cdr_bodysys="Body System Code"
			cdr_mdr="MDR in CDR data"
			cdr_mddate="Medical Diary Date in CDR data"
			cdr_imppri="Primary Impairment in CDR data"
			cdr_impsec="Secondary Impairment in CDR data";
	
	drop fromcdr fromssr;

	rename cdr_state=cdr_state_str; 
	rename n_firstpay_yr=n_firstpayyr;

	* Create new diagnosis variables (since original come from SSR);
	%macro rename(stub);
		rename 	n_diag1_&stub.=n_diag1ssr_&stub.
	%mend rename;

	%rename(none);
	%rename(infec);
	%rename(neo);
	%rename(endo);
	%rename(blood);
	%rename(mental);
	%rename(nerv);
	%rename(sense);
	%rename(circ);
	%rename(resp);
	%rename(dig);
	%rename(gu);
	%rename(preg);
	%rename(skin);
	%rename(musc);
	%rename(cong);
	%rename(nat);
	%rename(ill);
	%rename(inj);

	cdr_imppri=input(cdr_imppri,5.);
	hun_num=input(hun,9.);

	* CHANGE MISSING VALUES FOR HOUSEHOLD VARIABLES (DI & SSI RECEIPT) TO ZERO:
		THESE PEOPLE WEREN'T MATCHED TO MBR AND SSR WHICH MEANS NO RECEIPT;
	%macro miss(var);
		if &var._1985=. then &var._1985=0;
		if &var._1986=. then &var._1986=0;
		if &var._1987=. then &var._1987=0;
		if &var._1988=. then &var._1988=0;
		if &var._1989=. then &var._1989=0;
		if &var._1990=. then &var._1990=0;
		if &var._1991=. then &var._1991=0;
		if &var._1992=. then &var._1992=0;
		if &var._1993=. then &var._1993=0;
		if &var._1994=. then &var._1994=0;
		if &var._1995=. then &var._1995=0;
		if &var._1996=. then &var._1996=0;
		if &var._1997=. then &var._1997=0;
		if &var._1998=. then &var._1998=0;
		if &var._1999=. then &var._1999=0;
		if &var._2000=. then &var._2000=0;
		if &var._2001=. then &var._2001=0;
		if &var._2002=. then &var._2002=0;
		if &var._2003=. then &var._2003=0;
		if &var._2004=. then &var._2004=0;
		if &var._2005=. then &var._2005=0;
		if &var._2006=. then &var._2006=0;
		if &var._2007=. then &var._2007=0;
		if &var._2008=. then &var._2008=0;
		if &var._2009=. then &var._2009=0;
		if &var._2010=. then &var._2010=0;
		if &var._2011=. then &var._2011=0;
	%mend miss;

	%miss(n_hhmbayr);
	%miss(n_hhmbayr0206);
	%miss(n_hhdiindyr);
	%miss(n_hhdiindyr0206);
	%miss(n_hhcombpay);
	%miss(n_hhcombpay0206);
	%miss(n_hhonssi);
	%miss(n_hhonssi0206);
	%miss(n_sibpayall);
	%miss(n_sibnumon);
	%miss(n_par831di);
	%miss(n_par831di0206);
	%miss(n_par831ssi);
	%miss(n_par831ssi0206);
	%miss(n_kid831_di);
	%miss(n_kid831_ssi);

	%macro miss1992(var);
		if &var._1992=. then &var._1992=0;
		if &var._1993=. then &var._1993=0;
		if &var._1994=. then &var._1994=0;
		if &var._1995=. then &var._1995=0;
		if &var._1996=. then &var._1996=0;
		if &var._1997=. then &var._1997=0;
		if &var._1998=. then &var._1998=0;
		if &var._1999=. then &var._1999=0;
		if &var._2000=. then &var._2000=0;
		if &var._2001=. then &var._2001=0;
		if &var._2002=. then &var._2002=0;
		if &var._2003=. then &var._2003=0;
		if &var._2004=. then &var._2004=0;
		if &var._2005=. then &var._2005=0;
		if &var._2006=. then &var._2006=0;
		if &var._2007=. then &var._2007=0;
		if &var._2008=. then &var._2008=0;
		if &var._2009=. then &var._2009=0;
		if &var._2010=. then &var._2010=0;
		if &var._2011=. then &var._2011=0;
	%mend miss1992;

	%miss1992(n_sibappnum);

	if n_hhcombpay_2012=. then n_hhcombpay_2012=0;
	if n_hhcombpay0206_2012=. then n_hhcombpay0206_2012=0;
	if n_hhonssi_2012=. then n_hhonssi_2012=0;
	if n_hhonssi0206_2012=. then n_hhonssi0206_2012=0;
	if n_sibpayall_2012=. then n_sibpayall_2012=0;
	if n_sibnumon_2012=. then n_sibnumon_2012=0;
	if n_sibappnum_2012=. then n_sibappnum_2012=0;

run;

data manasi.kids_cdryrs0607_mergecdr_clean;
	set manasi.kids_cdryrs0607_mergecdr_clean;

* CREATE VARIABLES ON CDR ELIGIBILITY/RELEASE/COMPLETION ;

	* CDR hold dummy;
	if cdr_hold="Y" then n_cdr_hold=1;
		else if cdr_hold="N" then n_cdr_hold=0;

	* CDR release dummy;
	if (cdr_reldate>0 & cdr_reldate^=.) then n_cdr_rel=1;
		else n_cdr_rel=0;

	* CDR completed dummy ;
	if (cdr_open="N" & n_cdr_rel=1) then n_cdr_done = 1;
		else n_cdr_done = 0;
	

* CREATE/PREP COVARIATES;

	* Male dummy;
	if cdr_sex="M" then n_male=1;
		else if cdr_sex="F" then n_male=0;

	* State variable ;
	cdr_state = input(cdr_state_str,2.0);

	* YOB and first year already exist; 

	* Age at first payment ;
	n_firstpayage=n_firstpayyr-dobyy;

	* Create MDR dummies ;
	if cdr_mdr="3" then n_dibmdrcat=3;
		else if cdr_mdr="5" then n_dibmdrcat=5;
		else if cdr_mdr="7" then n_dibmdrcat=7;
	if n_dibmdrcat=. then n_dibmdrcat=99;

	* Body system variable ;
	if cdr_bodysys="@@" then cdr_bodysys=".";
	cdr_bodysys=input(cdr_bodysys,2.0);

	* Replace SSR diagnoses with OQP diagnoses;
	%macro imp(stub,low,high);
		if (cdr_imppri>=&low. & cdr_imppri<&high.) then n_diag1_&stub.=1;
			else n_diag1_&stub.=0;
		if (cdr_impsec>=&low. & cdr_impsec<&high.) then n_diag2_&stub.=1;
			else n_diag2_&stub.=0;
	%mend imp;

	if (cdr_imppri=.) then n_diag1_none=1;
		else n_diag1_none=0;
	if (cdr_impsec=.) then n_diag2_none=1;
		else n_diag2_none=0;

	%imp(infec,0,1400);
	%imp(neo,1400,2400);
	%imp(endo,2400,2800);
	%imp(blood,2800,2900);
	%imp(mental,2900,3200);
	%imp(nerv,3200,3600);
	%imp(sense,3600,3900);
	%imp(circ,3900,4600);
	%imp(resp,4600,5200);
	%imp(dig,5200,5800);
	%imp(gu,5800,6300);
	%imp(preg,6300,6800);
	%imp(skin,6800,7100);
	%imp(musc,7100,7400);
	%imp(cong,7400,7600);
	%imp(nat,7600,7800);
	%imp(ill,7800,8000);
	%imp(inj,8000,10000);

run;
